<?php
/***
 * Hipay线上充值抽奖
 */
$_GET['mid'] = 'DJEHBSUAAY';

ini_set("display_errors", "On");//开启错误显示
define('INDEX', true);
define('TaskName', 'Auto_Send_Red_Envelope');
require_once dirname(__FILE__) . '/../../Lib/Sing_core.php';
@include libfile('function/api');

DB::SetMainDB(DB_ESHOP);
$crontab = DB::fetch_first("select * from %t where task_name=%s", array('crontab_exec', TaskName));
if ($crontab) {
    $taskStatus = $crontab['task_status'];
    if ($taskStatus) {
        exit;
    } else {
        DB::SetMainDB(DB_ESHOP);
        DB::update('crontab_exec', array(
            'task_status' => 1,
            'task_count' => $crontab['task_count'] + 1,
            'begin_time' => time()
        ), array(
            'task_name' => TaskName
        ));
    }
} else {
    DB::SetMainDB(DB_ESHOP);
    DB::insert('crontab_exec', array(
        'task_name' => TaskName,
        'task_func' => TaskName,
        'task_status' => 1,
        'begin_time' => time(),
        'close_time' => 0,
        'interval_time' => 0,
        'task_count' => 1
    ));
}
DB::SetMainDB(DB_ESHOP);
$rs=DB::fetch_all(" select * from %t  where is_verify=0 and status =1 and coupon_id=1",array("es_coupon_log"));
print_r('Auto_Send_Red_Envelope------------'.serialize($rs));
foreach ($rs as $r) {
    DB::SetMainDB(DB_MAIN);
    $member=DB::fetch_first(" select * from %t  where id=%d",array("common_member",$r['member_id']));
    $return = Eshop_mch_pay($r['mid'],$member['openid'], $r['member_id'], $r['price'],$_check_name = 'FORCE_CHECK',$member['name'],$r['title'],$_tablename='',$_tableid='');
    if($return['result_code'] == 'success'){
        $update['status']=2;
        $update['is_verify']=1;
        DB::SetMainDB(DB_ESHOP);
        DB::update("es_coupon_log", $update);
    }
}

DB::SetMainDB(DB_ESHOP);
DB::update('crontab_exec', array(
    'task_status' => 0,
    'close_time' => time()
), array(
    'task_name' => TaskName
));

